package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.InvoiceDao;
import cn.itcast.dao.cargo.PackingDao;
import cn.itcast.dao.cargo.ShippingDao;
import cn.itcast.domain.cargo.Invoice;
import cn.itcast.domain.cargo.InvoiceExample;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.Shipping;
import cn.itcast.service.cargo.InvoiceService;
import cn.itcast.service.cargo.ShippingService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class InvoiceServiceImpl implements InvoiceService {
    @Autowired
    private InvoiceDao invoiceDao;
    @Autowired
    private ShippingDao shippingDao;
    @Autowired
    private PackingDao packingDao;
    @Autowired
    private ShippingService shippingService;

    @Override
    public int deleteByPrimaryKey(String invoiceId) {
        return invoiceDao.deleteByPrimaryKey(invoiceId);
    }

    @Override
    public int insert(Invoice record) {
        return 0;
    }

    @Override
    public int insertSelective(Invoice record) {
        return 0;
    }

    @Override
    public List<Invoice> selectByExample(InvoiceExample example) {
        return null;
    }

    @Override
    public Invoice selectByPrimaryKey(String invoiceId) {
        return invoiceDao.selectByPrimaryKey(invoiceId);
    }

    @Override
    public int updateByPrimaryKeySelective(Invoice record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(Invoice record) {
        return 0;
    }

    @Override
    public PageInfo findByPage(int pageNum, int pageSize, InvoiceExample invoiceExample) {
        PageHelper.startPage(pageNum,pageSize);
        List<Invoice> list = invoiceDao.selectByExample(invoiceExample);
        return new PageInfo(list);
    }

    @Override
    public void save(Invoice invoice, String id) {
        Shipping shipping = shippingDao.selectByPrimaryKey(id);
        shipping.setState(2);
        Packing packing = packingDao.selectByPrimaryKey(id);
        invoice.setState(0);
        invoice.setInvoiceId(id);
        invoice.setScNo(packing.getInvoiceNo());
        invoice.setBlNo(shipping.getConsignee());
        invoice.setTradeTerms("FOB");
        shippingService.update(shipping);
        invoiceDao.insertSelective(invoice);
    }

    @Override
    public void update(Invoice invoice) {
        invoiceDao.updateByPrimaryKeySelective(invoice);
    }
}
